//
//  HXCycleImageVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/8.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXCycleImageVC.h"
#import "HX_Macro.h"
#import "HXPlayTitleCell.h"
#import "HXCourseCCell.h"
#import "HXUserTCell.h"
#import "HXRedPickModel.h"
#import "HXHeaderImgModel.h"
#import "HXClassVideoModel.h"
#import "HXUserTalkModel.h"

@interface HXCycleImageVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) NSString *titles;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrNetid;
@property (nonatomic, assign) NSInteger addHeight;
/**
 *  数据源
 */
@property (nonatomic, strong) NSMutableArray *arrAll;
@property (nonatomic, strong) NSMutableArray *userArr;
@end
@implementation HXCycleImageVC
- (NSMutableArray *)arrAll {
    if (!_arrAll) {
        _arrAll = [NSMutableArray array];
    }
    return _arrAll;
}
- (NSMutableArray *)userArr {
    if (!_userArr) {
        _userArr = [NSMutableArray array];
    }
    return _userArr;
}
- (NSMutableArray *)arrNetid {
    if (!_arrNetid) {
        _arrNetid = [NSMutableArray array];
    }
    return _arrNetid;
}
- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [_tableView registerClass:[HXPlayTitleCell class] forCellReuseIdentifier:@"HXPlayTitleCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"HXUserTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXUserTCell"];
        _tableView.separatorStyle = NO;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.netId) {
    NSArray *string = [self.netId componentsSeparatedByString:@"#"];
    for (NSString *netStr in string) {
        [self.arrNetid addObject:netStr];
    }
        self.ID = self.arrNetid[1];
    }
    self.view.backgroundColor = COLORAUTO;
    [self getNetWorking];
    [self getUserNetWorking];
    [self tableView];
}
- (void)getUserNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"relate_id=%@&type=2&page=1&methodName=CommentList&size=20&version=4.40&", self.ID];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dic in arrData) {
          HXUserTalkModel *model = [HXUserTalkModel ModelWithDic:dic];
            [self.userArr addObject:model];
            [_tableView reloadData];
        }
    } Failure:^(NSError *error) { 
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.userArr.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXPlayTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXPlayTitleCell"];
        cell.backgroundColor = [UIColor whiteColor];
        HXRedPickModel *readModle = [self.arrAll firstObject];
        [cell.imageHeader sd_setImageWithURL:[NSURL URLWithString:readModle.series_image]];
        [cell.share setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
        cell.lableUp.text = [NSString stringWithFormat:@"更新到了：%ld集", readModle.episode];
        [cell.upBtn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
        cell.arrDataSource = readModle.data;
        cell.lableDisCount.text = [NSString stringWithFormat:@"%ld条评论", self.userArr.count];
        cell.addHeight = self.addHeight;
        cell.selectionStyle = 0;
        return cell;
    } else {
        HXUserTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXUserTCell"];
        HXUserTalkModel *talkModel = self.userArr[indexPath.row];
        [cell.headImg sd_setImageWithURL:[NSURL URLWithString:talkModel.head_img]placeholderImage:[UIImage imageNamed:@"userHeadImage"]];
        cell.headImg.layer.masksToBounds = YES;
        cell.labelName.text = talkModel.nick;
        cell.detail.text = talkModel.content;
        cell.titime.text = talkModel.create_time;
        cell.headImg.layer.cornerRadius = 40;
        cell.backgroundColor = [UIColor whiteColor]; 
        return cell;
    }
}
- (void)addBtn:(UIButton *)btn {
    HXRedPickModel *readModel = [self.arrAll firstObject];
    if ((readModel.data.count - 14) % 7 == 0) {
        self.addHeight = (readModel.data.count - 14) / 7 * (610 * 0.07);
    } else {
        self.addHeight = (readModel.data.count - 7) / 7 * (610 * 0.07);
    }
    [btn setImage:[UIImage imageNamed:@"xiangshang"] forState:UIControlStateNormal];
    [btn removeTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_tableView reloadData];
}
- (void)removeBtn:(UIButton *)btn {
    [btn setImage:[UIImage imageNamed:@"xiangxiayanzhan"] forState:UIControlStateNormal];
    self.addHeight = 0;
    [btn removeTarget:self action:@selector(removeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_tableView reloadData];
}

- (void)getNetWorking {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=CourseSeriesView&series_id=%@&version=4.40&", self.ID];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        self.navigationItem.title = dicData[@"series_name"];
        [self.arrAll addObject:[HXRedPickModel ModelWithDic:dicData]];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXRedPickModel *redModle = [self.arrAll firstObject];
    if (indexPath.section == 0) {
        if (redModle.data.count > 7 && redModle.data.count <= 14) {
            return 610 + (610 * 0.07);
        } else if (redModle.data.count > 14) {
            return 610 + (610 * 0.07) + self.addHeight;
        } else {
            return 610;
        }
    } else {
        return 100;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
